/* 
 * Javascript for the index.html
 */

// Alert message for Help button
function helpAlert() {
    helpMsg = "Uruk Hai\n"
        + "The Online UML Editor\n"
        + "v1.0\n"
        + "BME - AUT\n"
        + "Mészáros Kornél\n"
        + "Nagy Bálint\n"
        + "2012";
    window.alert(helpMsg);
}

function ShowOutput(str) {
    // Hide diagram view
    document.getElementById("diagramview").style.display="none";
    // Output str with Back button
    document.getElementById("xml_output").innerHTML = ''
            + '<button type="button" onclick="javascript: ShowOutputBack();">Back</button>'
            + '<br/>' + str;
    // Show xml output
    document.getElementById("xml_output").style.display = "inherit";
}

function ShowOutputBack() {
    // Display from now on
    document.getElementById("diagramview").style.display="";
    // Clear test div
    document.getElementById("xml_output").innerHTML = "";
    // Hide xml output
    document.getElementById("xml_output").style.display = "none";
}

// Function for adding FileUpload input element
// in Project explorer for loading project xml
function addFilePicker() {
    document.getElementById("projecttree").innerHTML = 
            '<input id="upload" type="file" name="modelfile"/>'
            + '<br/><button type ="button"' 
            + 'onclick="javascript: cancelFilePicker();"'
            + '>Cancel</button>';
    document.getElementById('upload').addEventListener('change', handleFileSelect, false);
}

// Function for bringing back the Project Explorer treeview
function cancelFilePicker() {
    project.buildTreeView();
}

// When an input file was added, this function called
// Input file -> string -> xml -> project
function handleFileSelect(evt) {
    // First of all: Backup
    // project: global value
    projectBackup = project;
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 

    if (f) {
        var r = new FileReader();
        r.onload = function(e) {
            try{
            var contents = e.target.result;
            if(f.type !== "text/xml")
              throw "Filetype not XML";

            //alert( "Got the file.n" 
            //      +"name: " + f.name + "\n"
            //      +"type: " + f.type + "\n"
            //      +"size: " + f.size + " bytes\n"
            //      + "starts with: " + contents.substr(1, contents.indexOf("n"))
            //      + contents);

            // String to XML
            try {
                var xmlDoc1;
                if (window.DOMParser)
                {
                    parser=new DOMParser();
                    xmlDoc1=parser.parseFromString(contents,"text/xml");
                }
                else // Internet Explorer
                {
                    xmlDoc1=new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc1.async=false;
                    xmlDoc1.loadXML(contents);
                }
            }
            catch (err1) {
//                throw err1;
                throw "Bad XML formatting";
            }

            // Load project from XML
            try{
                project.loadProjectFromXML(xmlDoc1);
                document.getElementById("currentproject")
                .innerHTML = "Project: " + project.name;
            }
            catch(err2) {
//                throw err2;
                throw "Missing data from project xml";
            }
          }
          catch (err){
              alert("Failed to load project \n" 
                      + "Cause: "
                      + err);
              project = projectBackup;
              project.switchToDiagram(project.diagrams[0]);
              project.buildTreeView();
          }

        }
        // Read the file as String
        r.readAsText(f);

    } else { 
      alert("Failed to load file");
    }

}

// Optimize height of the window
function resizeToFitWindow()
{
    var maxHeight = 1000;
    var menuBarHeight = 32;
    
    document.body.style.height =
            (Math.max(maxHeight, window.innerHeight))
            .toString() + 'px';
    
    // Menu bar: 32px
    // Others: innerHeight - 32px or at least
    /*
    var maxEditorwindowHeight = maxHeight-menuBarHeight;
    document.getElementById('projectexplorer').height =
            (Math.max(maxEditorwindowHeight/2, 
                (window.innerHeight-menuBarHeight)/2))
            .toString() + 'px';
    document.getElementById('editorwrapper').height =
            (Math.max(maxEditorwindowHeight/2, 
                (window.innerHeight-menuBarHeight)/2))
            .toString() + 'px';
    document.getElementById('diagramview').height =
            (Math.max(maxEditorwindowHeight, 
                window.innerHeight-menuBarHeight))
            .toString() + 'px';
    document.getElementById('toolbar').height =
            (Math.max(maxEditorwindowHeight, 
                window.innerHeight-menuBarHeight))
            .toString() + 'px';
    */
}

function welcomeToTheInternet() {
    // Easter egg
    var c = document.getElementById("mainCanvas");
    var ctx = c.getContext("2d");
    img = new Image();
    img.src = "http://www.lolbrary.com/content/594/welcome-to-the-internet-i-will-be-your-guide-25594.jpg";
    ctx.drawImage(img, 0, 0, 700, 525, -100, 0, 700, 500);
}

function notImplementedYetAlert() {
    window.alert("Not implemented yet :(");
}
